package cn.iocoder.yudao.module.scrm.dal.mysql.customer.customerrepository;


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.scrm.controller.admin.customer.vo.customerrepository.CustomerRepositoryRulePageReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.customerrepository.CustomerRepositoryRuleDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

/**
 * 客户库规则 Mapper
 *
 * @author admin
 */
@Mapper
public interface CustomerRepositoryRuleMapper extends BaseMapperX<CustomerRepositoryRuleDO> {

    //根据部门id查询规则信息
    default  CustomerRepositoryRuleDO getRepositoryRuleByDeptId(Long deptId) {
        return selectOne(new LambdaQueryWrapperX<CustomerRepositoryRuleDO>()
                .apply("FIND_IN_SET({0}, dept_ids) > 0", deptId));
    }


    default List<CustomerRepositoryRuleDO> selectListByCustomerRepositoryId(Integer customerRepositoryId) {
        return selectList(CustomerRepositoryRuleDO::getCustomerRepositoryId, customerRepositoryId);
    }

    default int deleteByCustomerRepositoryId(Integer customerRepositoryId) {
        return delete(CustomerRepositoryRuleDO::getCustomerRepositoryId, customerRepositoryId);
    }

    default PageResult<CustomerRepositoryRuleDO> selectPage(CustomerRepositoryRulePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CustomerRepositoryRuleDO>()
                .eqIfPresent(CustomerRepositoryRuleDO::getCustomerRepositoryId, reqVO.getCustomerRepositoryId())
                .eqIfPresent(CustomerRepositoryRuleDO::getDeptIds, reqVO.getDeptIds())
                .eqIfPresent(CustomerRepositoryRuleDO::getUserIds, reqVO.getUserIds())
                .likeIfPresent(CustomerRepositoryRuleDO::getRuleName, reqVO.getRuleName())
                .eqIfPresent(CustomerRepositoryRuleDO::getType, reqVO.getType())
                .eqIfPresent(CustomerRepositoryRuleDO::getCustomerCount, reqVO.getCustomerCount())
                .eqIfPresent(CustomerRepositoryRuleDO::getCustomerCountLock, reqVO.getCustomerCountLock())
                .betweenIfPresent(CustomerRepositoryRuleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CustomerRepositoryRuleDO::getId));
    }

}